<template>
	<div>
		<!-- 关联订单弹窗 -->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog  type="info" cancelText="关闭" confirmText="确定" title="关联订单" @confirm="dialogConfirm"
			:before-close="true" @close="closeDialog('alertDialog')">
			<view style="display: flex;flex-direction: column;width: 100%;">
				<uni-segmented-control :current="current" :values="items" style-type="button"
						active-color="#007aff" @clickItem="onClickItem" />
				<scroll-view scroll-top="0" scroll-y="true" style="height: 200px;">
					<view style="display: flex;flex-direction: column;width: 100%;">
						<uni-data-checkbox v-if="current === 0" mode="button" multiple v-model="order" :localdata="allOrderList"></uni-data-checkbox>
						<uni-data-checkbox v-else-if="current === 1"  mode="button" multiple v-model="order" :localdata="allOrderList"></uni-data-checkbox>
					</view>
				</scroll-view>
			</view>
			</uni-popup-dialog>
		</uni-popup>
	</div>
</template>

<script>
	import {
		getOrderList
	} from '@/api/system/order.js';
	import {getDicts} from '@/api/system/dict.js'
	export default {
		props: {
			data: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				formData:{},
				allOrderList: [],
				//返回的数据
				orderList:[],
				order:[],
				//运单状态
				status:[],
				items: ['线上', '线下'],
				current: 0,
				row:[]
			}
		},
		watch: {
			data: {
			　　handler(newValue, oldValue) {
			　　　	this.formData=newValue
			　　},
			　　deep: true
			},
		},
		methods: {
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
				//加载订单列表
				if(this.current===0)
				{
					this.order=[]
					this.getOrderList_online()
				}
				else
				{
					this.order=[]
					this.getOrderList()
				}
			},
			//关闭弹窗
			closeDialog(name){
				this.$refs[name].close()
			},
			//关联订单
			correlationWaybill(row){
				this.row=row
				//加载订单列表
				if(this.current===0)
				{
					this.order=[]
					this.getOrderList_online()
				}
				else
				{
					this.order=[]
					this.getOrderList()
				}
				this.$refs.alertDialog.open()
			},
			//线上订单
			getOrderList_online(row){
				//检索相同收发货方、收发货地址的订单
				let queryParams={}
				queryParams.shipId=this.formData.shipId
				queryParams.deliveryId=this.formData.deliveryId
				queryParams.shipDistrictId=this.formData.shipDistrictId
				queryParams.deliveryDistrictId=this.formData.deliveryDistrictId
				//检索未关联运单的订单
				queryParams.queryType=1  //1.未关联运单  2.已关联运单
				//goodOrderType订单类型：0.线下订单 1.线上订单
				getOrderList({pageNum:1,pageSize:1000,...queryParams,goodOrderType:1}).then(res => {
					uni.hideLoading()
					if (res.code == '200') {
						this.allOrderList=res.rows.map(item=>{
							let flag=false
							this.row.forEach(item2=>{
								if(item2==item.pickupOrderNo+'('+item.orderCode+')')
								{
									flag=true
								}
							})
							return {
								value:JSON.stringify(item),
								text:'订单号:'+item.orderCode+' 提货单号:'+item.pickupOrderNo+' '+item.shipName+'->'+item.deliveryName,
								disable: flag
							}
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			//线下订单
			getOrderList(){
				//检索相同收发货方、收发货地址的订单
				let queryParams={}
				queryParams.shipId=this.formData.shipId
				queryParams.deliveryId=this.formData.deliveryId
				queryParams.shipDistrictId=this.formData.shipDistrictId
				queryParams.deliveryDistrictId=this.formData.deliveryDistrictId
				queryParams.queryType=1  //1.未关联运单  2.已关联运单
				//goodOrderType订单类型：0.线下订单 1.线上订单
				getOrderList({pageNum:1,pageSize:1000,...queryParams,goodOrderType:0}).then(res => {
					uni.hideLoading()
					if (res.code == '200') {
						this.allOrderList=res.rows.map(item=>{
							let flag=false
							this.row.forEach(item2=>{
								if(item2==item.pickupOrderNo+'('+item.orderCode+')')
								{
									flag=true
								}
							})
							return {
								value:JSON.stringify(item),
								text:'订单号:'+item.orderCode+' 提货单号:'+item.pickupOrderNo+' '+item.shipName+'->'+item.deliveryName,
								disable: flag
							}
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			//确认设置关联订单
			dialogConfirm() {
				if(this.order.length)
				{
					let orderInfo=JSON.parse(this.order[0])
					let a=this.formData.shipId== null || this.formData.shipId=== "" || this.formData.shipId=== "undefined"
					let b=this.formData.deliveryId== null || this.formData.deliveryId=== "" || this.formData.deliveryId=== "undefined"
					let c=this.formData.shipDistrictId== null || this.formData.shipDistrictId=== "" || this.formData.shipDistrictId=== "undefined"
					let d=this.formData.deliveryDistrictId== null || this.formData.deliveryDistrictId=== "" || this.formData.deliveryDistrictId=== "undefined"
					//如果是在收发货方数据全为空时关联订单
					if(a&&b&&c&&d)
					{
						if(this.order.length===1)
						{
							if(orderInfo.status!=='110')
							{
								uni.showToast({
									icon:'none',
									title:'请选择未取消的订单'
								})
								return
							}
							if(orderInfo.goodSourceList)
							{
								orderInfo.goodSourceList.forEach(item=>{
									item.pickupOrderNo=orderInfo.pickupOrderNo
									item.goodsType=''+item.goodsType
									item.packingType=''+item.packingType
									item.originGoodOrderId=orderInfo.goodOrderId
									item.orderCode=orderInfo.orderCode //订单号
								})
							}
							else orderInfo.goodSourceList=[]

							let form={
								shipId:orderInfo.shipId,  //发货方id
								shipName:orderInfo.shipName,  //发货方名称
								shipNetId:orderInfo.shipNetId, //发货方网点id
								shipDistrictId:orderInfo.originDistractId,  //发货方省市区
								shipDistrictName:orderInfo.originDistractName,  //发货方省市区名称
								shipAddress:orderInfo.originAddress,  //发货方详细地址
								shipContacts:orderInfo.shipContacts,  //发货方联系人
								shipContactTel:orderInfo.shipContactTel,  //发货方联系电话
								deliveryId:orderInfo.deliveryId,  //收货方id
								deliveryName:orderInfo.deliveryName,  //收货方名称
								//deliveryNetId:this.ids[0].deliveryNetId, //收货方网点id
								deliveryDistrictId:orderInfo.destinationDistractId,  //收货方省市区
								deliveryDistrictName:orderInfo.destinationDistractName,  //收货方省市区名称
								deliveryAddress:orderInfo.destinationAddress,  //收货方详细地址
								deliveryContacts:orderInfo.deliveryContacts,  //收货方联系人
								deliveryContactTel:orderInfo.deliveryContactTel,  //收货方联系电话
								shipPlanTime:orderInfo.pickupTime,  //计划提货时间
								shipPickUpWay:'3',  //发货提货方式（发货地直达）
								deliveryPlanTime:orderInfo.arrivalTime,  //计划到达时间
								deliveryPickUpWay:'3',  //到达提货方式（收货地直达）
								goodSourceList:orderInfo.goodSourceList||[],  //货物信息
								orderCodeList:[orderInfo.pickupOrderNo+'('+orderInfo.orderCode+')'],    //提货单号
								waybillValueAdd:{
									parcelInsuranceFee:'',
									receiptFee:'',
									warehouseFee:'',
									loadingFee:'',
									unloadFee:'',
									drayage:'',
									otherFee:''
								},
								goodOrderIdList:[orderInfo.goodOrderId]     //记录订单号
							}
							this.$emit('correlationWaybill',form)
							this.$refs.alertDialog.close()
							return
						}
						// this.order.forEach(item=>{
						// 	let order=JSON.parse(item)
						// 	this.orderList.push({
						// 		value: order.goodOrderId,
						// 		id: order.pickupOrderNo,
						// 		orderInfo:order,
						// 	})
						// })
						// 收发货方、地址全相等
						if(this.order.every(item=>JSON.parse(item).shipId === orderInfo.shipId&&JSON.parse(item).deliveryId === orderInfo.deliveryId&&JSON.parse(item).originDistractId === orderInfo.originDistractId&&JSON.parse(item).destinationDistractId === orderInfo.destinationDistractId))
						{
							if(!this.order.every(item=>JSON.parse(item).status==='110'))
							{
								uni.showToast({
									icon:'none',
									title:'请选择未取消的订单'
								})
								return
							}
							let goods=[]
							this.order.forEach(item=>{
							  let item_=JSON.parse(item)
							  if(item_.goodSourceList)
							  {
							    item_.goodSourceList.forEach(i=>{
							      i.pickupOrderNo=item_.pickupOrderNo
							      i.goodsType=''+i.goodsType
							      i.packingType=''+i.packingType
							      i.originGoodOrderId=item_.goodOrderId
								  i.orderCode=item_.orderCode //订单号
							    })
							  }
							  else item_.goodSourceList=[]
							  goods.push(...item_.goodSourceList)
							})
							let form={
							  shipId:orderInfo.shipId,  //发货方id
							  shipName:orderInfo.shipName,  //发货方名称
							  shipNetId:orderInfo.shipNetId, //发货方网点id
							  shipDistrictId:orderInfo.originDistractId,  //发货方省市区
							  shipDistrictName:orderInfo.originDistractName,  //发货方省市区名称
							  shipAddress:orderInfo.originAddress,  //发货方详细地址
							  shipContacts:orderInfo.shipContacts,  //发货方联系人
							  shipContactTel:orderInfo.shipContactTel,  //发货方联系电话
							  deliveryId:orderInfo.deliveryId,  //收货方id
							  deliveryName:orderInfo.deliveryName,  //收货方名称
							  //deliveryNetId:orderInfo.deliveryNetId, //收货方网点id
							  deliveryDistrictId:orderInfo.destinationDistractId,  //收货方省市区
							  deliveryDistrictName:orderInfo.destinationDistractName,  //收货方省市区名称
							  deliveryAddress:orderInfo.destinationAddress,  //收货方详细地址
							  deliveryContacts:orderInfo.deliveryContacts,  //收货方联系人
							  deliveryContactTel:orderInfo.deliveryContactTel,  //收货方联系电话
							  shipPlanTime:orderInfo.pickupTime,  //计划提货时间
							  shipPickUpWay:'3',  //发货提货方式（发货地直达）
							  deliveryPlanTime:orderInfo.arrivalTime,  //计划到达时间
							  deliveryPickUpWay:'3',  //到达提货方式（收货地直达）
							  goodSourceList:goods,  //货物信息
							  orderCodeList:this.order.map(i=>JSON.parse(i).pickupOrderNo+'('+JSON.parse(i).orderCode+')'),    //提货单号
							  waybillValueAdd:{
							  	parcelInsuranceFee:'',
							  	receiptFee:'',
							  	warehouseFee:'',
							  	loadingFee:'',
							  	unloadFee:'',
							  	drayage:'',
							  	otherFee:''
							  },
							  goodOrderIdList:this.order.map(i=>JSON.parse(i).goodOrderId)     //记录订单号
							}
							this.$emit('correlationWaybill',form)
							this.$refs.alertDialog.close()
						}
						else
						{
							uni.showToast({
								title: '请选择相同收发货方、地址的订单',
								icon: 'none'
							})
						}
					}
					//如果是在收发货方数据存在时关联订单
					else
					{
						if(this.order.length===1)
						{
						  // 收发货方、地址与运单收货方数据相等
						  if(orderInfo.shipId === this.formData.shipId&&orderInfo.deliveryId=== this.formData.deliveryId&&orderInfo.originDistractId=== this.formData.shipDistrictId&&orderInfo.destinationDistractId=== this.formData.deliveryDistrictId){
						    if(orderInfo.status!=='110')
						    {
						    	uni.showToast({
						    		icon:'none',
						    		title:'请选择未取消的订单'
						    	})
						    	return
						    }
							if(orderInfo.goodSourceList)
						    {
						      orderInfo.goodSourceList.forEach(item=>{
						        item.pickupOrderNo=orderInfo.pickupOrderNo
						        item.goodsType=''+item.goodsType
						        item.packingType=''+item.packingType
						        item.originGoodOrderId=orderInfo.goodOrderId
								item.orderCode=orderInfo.orderCode //订单号
						      })
						    }
							else orderInfo.goodSourceList=[]
						    let form={
						      shipId:orderInfo.shipId,  //发货方id
						      shipName:orderInfo.shipName,  //发货方名称
						      shipNetId:orderInfo.shipNetId, //发货方网点id
						      shipDistrictId:orderInfo.originDistractId,  //发货方省市区
						      shipDistrictName:orderInfo.originDistractName,  //发货方省市区名称
						      shipAddress:orderInfo.originAddress,  //发货方详细地址
						      shipContacts:orderInfo.shipContacts,  //发货方联系人
						      shipContactTel:orderInfo.shipContactTel,  //发货方联系电话
						      deliveryId:orderInfo.deliveryId,  //收货方id
						      deliveryName:orderInfo.deliveryName,  //收货方名称
						      //deliveryNetId:orderInfo.deliveryNetId, //收货方网点id
						      deliveryDistrictId:orderInfo.destinationDistractId,  //收货方省市区
						      deliveryDistrictName:orderInfo.destinationDistractName,  //收货方省市区名称
						      deliveryAddress:orderInfo.destinationAddress,  //收货方详细地址
						      deliveryContacts:orderInfo.deliveryContacts,  //收货方联系人
						      deliveryContactTel:orderInfo.deliveryContactTel,  //收货方联系电话
						      shipPlanTime:orderInfo.pickupTime,  //计划提货时间
						      shipPickUpWay:'3',  //发货提货方式（发货地直达）
						      deliveryPlanTime:orderInfo.arrivalTime,  //计划到达时间
						      deliveryPickUpWay:'3',  //到达提货方式（收货地直达）
						      goodSourceList:orderInfo.goodSourceList||[],  //货物信息
						      orderCodeList:[orderInfo.pickupOrderNo+'('+orderInfo.orderCode+')'],    //提货单号
							  waybillValueAdd:{
							  	parcelInsuranceFee:'',
							  	receiptFee:'',
							  	warehouseFee:'',
							  	loadingFee:'',
							  	unloadFee:'',
							  	drayage:'',
							  	otherFee:''
							  },
							  goodOrderIdList:[orderInfo.goodOrderId]     //记录订单号
						    }
					
						    this.$emit('correlationWaybill',form)
							this.$refs.alertDialog.close()
						    return
						  }
						  else  //如果不相等
						  {
						    uni.showToast({
						    	title: '请选择相同收发货方、地址的订单',
						    	icon: 'none'
						    })
						    return
						  }
						}
						// 收发货方、地址与运单收货方数据相等
						if(this.order.every(item=>(JSON.parse(item).shipId === orderInfo.shipId&&JSON.parse(item).shipId === this.formData.shipId)&&(JSON.parse(item).deliveryId === orderInfo.deliveryId&&JSON.parse(item).deliveryId === this.formData.deliveryId)&&(JSON.parse(item).originDistractId === orderInfo.originDistractId&&JSON.parse(item).originDistractId === this.formData.shipDistrictId)&&(JSON.parse(item).destinationDistractId === orderInfo.destinationDistractId&&JSON.parse(item).destinationDistractId === this.formData.deliveryDistrictId))){
						  if(!this.order.every(item=>JSON.parse(item).status==='110'))
						  {
						  	uni.showToast({
						  		icon:'none',
						  		title:'请选择未取消的订单'
						  	})
						  	return
						  }
						  let goods=[]
						  this.order.forEach(item=>{
						    let item_=JSON.parse(item)
						    if(item_.goodSourceList)
						    {
						      item_.goodSourceList.forEach(i=>{
						        i.pickupOrderNo=item_.pickupOrderNo
						        i.goodsType=''+i.goodsType
						        i.packingType=''+i.packingType
						        i.originGoodOrderId=item_.goodOrderId
								i.orderCode=item_.orderCode //订单号
						      })
						    }
							else item_.goodSourceList=[]
						    goods.push(...item_.goodSourceList)
						  })
						  let form={
						    shipId:orderInfo.shipId,  //发货方id
						    shipName:orderInfo.shipName,  //发货方名称
						    shipNetId:orderInfo.shipNetId, //发货方网点id
						    shipDistrictId:orderInfo.originDistractId,  //发货方省市区
						    shipDistrictName:orderInfo.originDistractName,  //发货方省市区名称
						    shipAddress:orderInfo.originAddress,  //发货方详细地址
						    shipContacts:orderInfo.shipContacts,  //发货方联系人
						    shipContactTel:orderInfo.shipContactTel,  //发货方联系电话
						    deliveryId:orderInfo.deliveryId,  //收货方id
						    deliveryName:orderInfo.deliveryName,  //收货方名称
						    //deliveryNetId:orderInfo.deliveryNetId, //收货方网点id
						    deliveryDistrictId:orderInfo.destinationDistractId,  //收货方省市区
						    deliveryDistrictName:orderInfo.destinationDistractName,  //收货方省市区名称
						    deliveryAddress:orderInfo.destinationAddress,  //收货方详细地址
						    deliveryContacts:orderInfo.deliveryContacts,  //收货方联系人
						    deliveryContactTel:orderInfo.deliveryContactTel,  //收货方联系电话
						    shipPlanTime:orderInfo.pickupTime,  //计划提货时间
						    shipPickUpWay:'3',  //发货提货方式（发货地直达）
						    deliveryPlanTime:orderInfo.arrivalTime,  //计划到达时间
						    deliveryPickUpWay:'3',  //到达提货方式（收货地直达）
						    goodSourceList:goods,  //货物信息
						    orderCodeList:this.order.map(i=>JSON.parse(i).pickupOrderNo+'('+JSON.parse(i).orderCode+')'),    //提货单号
							waybillValueAdd:{
								parcelInsuranceFee:'',
								receiptFee:'',
								warehouseFee:'',
								loadingFee:'',
								unloadFee:'',
								drayage:'',
								otherFee:''
							},
							goodOrderIdList:this.order.map(i=>JSON.parse(i).goodOrderId)     //记录订单号
						  }
						  this.$emit('correlationWaybill',form)
						  this.$refs.alertDialog.close()
						}
						else
						{
						  uni.showToast({
						  	title: '请选择相同收发货方、地址的订单',
						  	icon: 'none'
						  })
						}
					}
				}
				else
				{
					uni.showToast({
						title: '请选择相同收发货方、地址的订单',
						icon: 'none'
					})
				}
			},
		},
	}
</script>

<style scoped>

</style>